/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.palituxd.tools.swing;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 *
 * @author PPATOCHE
 */
public class TextoDocumento extends PlainDocument {

    private int cantidadMaxCaracteres;
    private boolean soloMayusculas;
    private boolean soloNumeros;

    public TextoDocumento(int cantidadMaxCaracteres, boolean soloMayusculas, boolean soloNumeros) {
        this.cantidadMaxCaracteres = cantidadMaxCaracteres;
        this.soloMayusculas = soloMayusculas;
        this.soloNumeros = soloNumeros;
    }

    @Override
    public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
        if (str == null) {
            return;
        }

        if (soloNumeros) {
            for (int i = 0; i < str.length(); i++) {
                if (!Character.isDigit(str.charAt(i))) {
                    return;
                }
            }
        }
        
        if(soloMayusculas){
            str = str.toUpperCase();
        }

        if ((getLength() + str.length()) <= cantidadMaxCaracteres) {
            super.insertString(offset, str, attr);
        }
    }
}
